<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;

use app\common\controller\Alisms;
use app\common\controller\Smscm;
use app\common\model\Users;
use app\web\validate\User as UserValidate;
use think\Db;

/**
 * 前台首页控制器
 * @package app\index\controller
 */
class Register extends Auth
{
    public $noNeedLogin = ['login','index','findPassword','activation'];

    /**
     * 账号密码登录
     */
    public function login()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['phone']) || empty($getData['password'])) {
                $this->result('缺少必要的参数', '', 204);
            }
            $userModel = new Users();
            $code = $userModel->checkUser($getData);
            switch ($code) {
                case 2000;
                    $token = token_rand();
                    Users::where(['phone'=>$getData['phone'],'group'=>'2'])->update(['pc_token'=>$token]);
                    $userInfo = Users::where('phone', $getData['phone'])->where('group','2')->field('id,phone,real_name,pc_token as  login_token,avatar,store_id,status')->find();
                    $storeRes = Db::name('qt_store')->where('id',$userInfo['store_id'])->find();
                    $userInfo['start_time'] = $storeRes['start_time'];
                    $userInfo['store_name'] = $storeRes['name'];
                    unset($userInfo->password);
                    unset($userInfo->safeword);
                    $userInfo['login_token'] = $token;
                    $this->result('登录成功', $userInfo, 200);
                    break;
                case 10001;
                    $this->result('用户不存在，请先去注册', '', 205);
                    break;
                case 10002;

                    $this->result('账户被禁用，请联系客服', '', 206);
                    break;
                case 10003;
                    $this->result('账号密码错误', '', 207);
                    break;
            }

        }
        $this->result('无效的请求', '', 204);

    }



    /**
     * 找回密码
     */
    public function findPassword()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $validate = new UserValidate();
            if (!$validate->scene('update')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $userId = Users::where('phone', $getData['phone'])->value('id');
            if (empty($userId)) {
                $this->result('当前手机号暂未注册', '', 204);
            }
            if ($getData['password'] == $getData['new_password']) {
                $sms = new Smscm();
                $checkPhone = $sms->check_smscode($getData['phone'], $getData['code'], 2);
                if ($checkPhone['code'] == 200) {
                    $newPassword = pwd($getData['password'], $userId);
                    $update = Users::where('id', $userId)->update(['password' => $newPassword]);
                    if ($update) {
                        $this->result('密码修改成功', '', 200);
                    }
                }
                $this->result('密码修改失败', '', 204);
            }
            $this->result('两次密码不一致', '', 204);
        }
    }




    /**
     * 修改手机号
     */
    public function save_phone()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['phone']) ||empty($getData['code']) || empty($getData['new_phone'])){
                $this->result('缺少参数', '', 204);
            }
            $userId = Users::where('phone', $getData['phone'])->value('id');
            if (empty($userId)) {
                $this->result('当前用户暂未注册', '', 204);
            }
            $sms = new Smscm();
            $checkPhone = $sms->check_smscode($getData['phone'], $getData['code'], 3);
            if ($checkPhone['code'] == 200) {
                $update = Users::where('id', $userId)->update(['phone' => $getData['new_phone'],'login_token'=>token_rand()]);
                if ($update) {
                    $this->result('手机修改成功', '', 200);
                }
            }
            $this->result('手机修改失败', '', 204);

        }
    }


    /**
     * 激活楼面和技师
     */
    public function activation(){
        $getData = $this->request->post();
        $aliSms = new Alisms();
        $result = $aliSms->check_smscode($getData['phone'],'123456','3');
        if($result == true){
            //更新用户状态
            $find = Db::name('qt_users')->where(['phone'=>$getData['phone']])->find();
            if(empty($find)){
                $this->result('该用户没有注册', '', 204);
            }
            if($find['store_id'] != $getData['store_id']){
                $this->result('请扫描当前门店二维码', '', 204);
            }
            $update = Db::name('qt_users')->where(['phone'=>$getData['phone']])->update(['status'=>'0']);
            $userInfo = Db::name('qt_users')->field('id')->where(['phone'=>$getData['phone']])->find();
            if($update !== false){
                //获取门店记录
                $userStore = Db::name('qt_user_store')
                    ->where(['user_id'=>$userInfo['id'],'store'=>$this->userInfo['store_id'],'status'=>'0'])
                    ->find();
                //更新状态
                Db::name('qt_user_store')->where(['id'=>$userStore['id']])->update(['status'=>'1']);
                $this->result('激活成功', '', 200);
            }else{
                $this->result('激活失败', '', 204);
            }
        }else{
            $this->result('短信验证失败', '', 204);
        }
    }
    


}
